﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using School.Common;

namespace School.Pages.Admission
{
    public partial class NewStudent : System.Web.UI.Page
    {
        SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1009) == true)
                {
                    if (!IsPostBack)
                    {
                        ClearControls();
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                var student = new TE_Student();

                student.Status_Id = int.Parse(LstStatus.SelectedValue);
                student.Ar_FirstName = TxtFirstName.Text;
                student.Ar_SecondName = TxtSecondName.Text;
                student.Ar_ThirdNmae = TxtThirdName.Text;
                student.Ar_FourthName = TxtForuthName.Text;
                student.Campus_Id = long.Parse(LstCampuses.SelectedValue);
                student.Certificate_Id = long.Parse(LstCertificates.SelectedValue);
                student.Stage_Id = long.Parse(LstStages.SelectedValue);
                student.Grade_ID = long.Parse(LstGrades.SelectedValue);
                student.FileNumber = 0;
                student.RegistrationNumber = 0;
                student.Gregorian_BirthDate = DateTime.Now;

                db.AddToTE_Student(student);
                db.SaveChanges();
                LblMessage.ForeColor = System.Drawing.Color.Green;
                LblMessage.Text = "تم حفظ الطالب بنجاح";
                ClearControls();
            }
            catch
            {
                LblMessage.ForeColor = System.Drawing.Color.Red;
                LblMessage.Text = "حدث خطأ، برجاء التأكد من إدخال جميع بيانات الطالب بشكل صحيح";
            }
        }

        private void ClearControls()
        {
            LstCampuses.DataSource = Structure.GetCampuses();
            LstCampuses.DataTextField = "Name";
            LstCampuses.DataValueField = "ID";
            LstCampuses.DataBind();

            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstStatus.DataSource = from x in db.ADM_Status_Type select x;
            LstStatus.DataValueField = "Id";
            LstStatus.DataTextField = "Name";
            LstStatus.DataBind();
            LstStatus.SelectedValue = "5";

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
        }
    }
}